package cn.crm.Service.Impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.poi.ss.formula.functions.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.crm.Service.IUserService;
import cn.crm.common.ServerResponse;
import cn.crm.entity.Permission;
import cn.crm.entity.Role;
import cn.crm.entity.User;
import cn.crm.entity.UserRole;
import cn.crm.mapper.PermissionMapper;
import cn.crm.mapper.RoleMapper;
import cn.crm.mapper.UserMapper;
import cn.crm.mapper.UserRoleMapper;
import cn.crm.util.UserContext;
import cn.crm.vo.LayuiMVO;
@Service
public class UserServiceImpl implements IUserService{

	@Autowired
	private UserMapper usermapper;
	@Autowired
	private UserRoleMapper userrolemapper;
	@Autowired
	private RoleMapper rolemapper;
	@Autowired
	private PermissionMapper permissionmapper;
	
	@Override
	public ServerResponse pageList(Integer page, Integer limit,User user) {
		// TODO Auto-generated method stub
		PageHelper.startPage(page, limit);
		List list = usermapper.pageList(user);
		PageInfo pageInfo = new PageInfo<>(list);
		Integer count = (int) pageInfo.getTotal();
		return ServerResponse.createSuccess("查询成功", count, list);
	}

	@Override
	public ServerResponse deleteAll(String ids) {
		// TODO Auto-generated method stub
		String[] array = ids.split(",");
		int count = usermapper.deleteAll(array);
		if (count == array.length) {
			return ServerResponse.createSuccess("删除成功");
		}else{
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse insert(User user, String roles) {
			
		int count = usermapper.insert(user);
		if (count == 1) {
		String[] roleIds = roles.split(",");
		for (String roleId : roleIds) {
			UserRole userRole = new UserRole(user.getId(), Integer.parseInt(roleId));
			userrolemapper.insert(userRole);
		}
		return ServerResponse.createSuccess("添加成功");
		}else {
			return ServerResponse.createError("添加失败");
		}
	}

	@Override
	public ServerResponse login(String name, String password,HttpSession session) {
		int count = usermapper.selectcount(name);
		if (count == 0) {
			return ServerResponse.createError("用户名不存在");
		}
		User user = usermapper.selectpassword(name,password);
		if (user == null) {
			return ServerResponse.createError("用户密码错误");
		}
		UserContext.session = session;
		UserContext.session.setAttribute(UserContext.USER_IN_SESSION, user);
		List<Permission> permissions = permissionmapper.selectByuserId(user.getId());
		UserContext.session.setAttribute(UserContext.PERMISSION_IN_SESSION, permissions);
		return ServerResponse.createSuccess("登陆成功");
	}

	@Override
	public ServerResponse deleteById(Integer id) {
		int count = usermapper.deleteByPrimaryKey(id);
		if (count == 1) {
			return ServerResponse.createSuccess("删除成功");
		}else {
			
			return ServerResponse.createError("删除失败");
		}
	}

	@Override
	public ServerResponse selectRolesandUsers(Integer userId) {
		// TODO Auto-generated method stub
		/**
		 * 我们在这里定义一个map集合最后以resp的形式返还回去，
		 * 将map里面放置user信息，roles已经被选中的信息，以及总数
		 */
		Map map = new HashMap<>();
		User user = usermapper.selectByPrimaryKey(userId);
		map.put("user", user);
		List<LayuiMVO> layuiMVOs = new ArrayList<>();
		List<Role> pageList = rolemapper.pageList(new Role());
		for (Role role : pageList) {
			LayuiMVO layuiMVO = new LayuiMVO();
			layuiMVO.setId(role.getId());
			layuiMVO.setName(role.getSn());
			layuiMVO.setStatus(1);
			layuiMVOs.add(layuiMVO);
		}
		map.put("LayuiMvo", layuiMVOs);
		List<Integer> roles = userrolemapper.selectByUserId(userId);
		map.put("values", roles.toArray());
		return ServerResponse.createSuccess("查询成功", map);
	}

	@Override
	public ServerResponse update(User user, String roles) {
		@SuppressWarnings("unused")
		int count = usermapper.updateByPrimaryKeySelective(user);
		int userId = user.getId();
		userrolemapper.deleteById(userId);
		String[] role = roles.split(",");
		for (String roleId : role) {
			UserRole userRole = new UserRole(userId, Integer.parseInt(roleId));
			userrolemapper.insert(userRole);
		}
		
		return ServerResponse.createSuccess("修改成功");
	}

	@Override
	public ServerResponse selectIds() {
		// TODO Auto-generated method stub
		List list = usermapper.pageList(new User());
		return ServerResponse.createSuccess("", list);
	}

}
